package eclipse.client;
import javax.media.opengl.*;
import javax.media.opengl.jgloo.jgl2d.*;
/**
 *The GameLogic class handles the drawing of any higher-level Graphical objects.
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class GameLogic
{
	/**
	 * Draws all player object to the OpenGL surface.
	 * @param gl OpenGL Object representing what is drawn.
	 */
	public static final void drawPlayers(GL gl)
	{
		// index of the sprite used
		int spriteID = 0;
		// direction and frame, used for pulling the image really
		int dir, frame;
		
		//first, determine if the player array exists
		if(Database.player == null)
		{
			//return because the player array does not exist (yet)
			return;
		}
		
		//scan through each player to confirm they exist
		for(int i = 0; i < Database.MAX_PLAYERS; i++)
		{
			//build a sprite Object to simplify code
			final JGLSprite sprite = Database.player[i].character.sprite;
			
			//get the sprite ID from the player's character object
			spriteID 	= Database.player[i].character.cSprite;
			//get the player's direction from the character Object
			dir			= Database.player[i].character.cDir;
			//get the player's direction from the frame Object
			frame		= Database.player[i].character.cFrame;
			
			//re-apply the texture
			//TODO: change to nx8
			sprite.setTexture(Database.tSpriteSheet[spriteID], 4, 4);
			//set the sprite using the frame/dir gained earlier
			sprite.setSprite(frame, dir);
			
			//start an OpenGL group
			gl.glPushMatrix();		
				//push the object by using the Player's offsets
				gl.glTranslatef(
						//x-position of the player
						Database.player[i].character.cPos.x,
						//y-position of the player
						Database.player[i].character.cPos.y,
						//z-position of the player, z-position is the height on the map
						Database.player[i].character.cPos.z);
				//this was the original code, it is static and therefore not really recommended
				//GLPrimitives.drawSprite(gl, Database.tSpriteSheet[spriteID], frame, dir, 4);	
				//tell the Player's sprite to draw itself
				sprite.render(gl);
			//end the OpenGL group
			gl.glPopMatrix();
		}
	}
}
